/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.handler.voicedcommandhandlers;

import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.entity.eventengine.EventManager;
import net.sf.l2j.gameserver.model.entity.eventengine.EventPlayer;
import net.sf.l2j.gameserver.model.entity.eventengine.EventState;

/**
 * @author Anarchy
 *
 */
public class EventCommands implements IVoicedCommandHandler
{
	private static final String[] VOICED_COMMANDS = { "joinevent", "leaveevent" };
	
	@Override
	public boolean useVoicedCommand(String command, L2PcInstance activeChar)
	{
		if (command.equals("joinevent"))
		{
			if (!EventManager.getInstance().isEventActive())
			{
				activeChar.sendMessage("There is no event currently running.");
				return false;
			}
			if (EventManager.getInstance().getActiveEvent()._state != EventState.REGISTERING)
			{
				activeChar.sendMessage("You may not register while the event is running.");
				return false;
			}
			if (EventManager.getInstance().getActiveEvent()._playersObjs.contains(activeChar))
			{
				activeChar.sendMessage("You have already registered for the event.");
				return false;
			}
			
			EventManager.getInstance().getActiveEvent()._players.add(new EventPlayer(activeChar, EventManager.getInstance().getActiveEvent()));
			EventManager.getInstance().getActiveEvent()._playersObjs.add(activeChar);
			activeChar.sendMessage("You have successfully registered for the event.");
		}
		
		if (command.equals("leaveevent"))
		{
			if (!EventManager.getInstance().isEventActive())
			{
				activeChar.sendMessage("There is no event currently running.");
				return false;
			}
			if (EventManager.getInstance().getActiveEvent()._state != EventState.REGISTERING)
			{
				activeChar.sendMessage("You may not unregister while the event is running.");
				return false;
			}
			if (!EventManager.getInstance().getActiveEvent()._playersObjs.contains(activeChar))
			{
				activeChar.sendMessage("You have not registered for the event.");
				return false;
			}
			
			EventManager.getInstance().getActiveEvent()._players.remove(EventManager.getInstance().getActiveEvent().getEventPlayerObject(activeChar));
			EventManager.getInstance().getActiveEvent()._playersObjs.remove(activeChar);
			activeChar.sendMessage("You have successfully unregistered from the event.");
		}
		
		return true;
	}

	@Override
	public String[] getVoicedCommandList()
	{
		return VOICED_COMMANDS;
	}
	
}
